package com.example.animatedemo;

import com.example.myslidingmenu.AnimationCreator;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.LayoutAnimationController;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.Scroller;

public class SlideMenuDemo extends ViewGroup {
	
	public static final int SLIDING_WINDOW = 0;
	public static final int SLIDING_CONTENT = 1;
	private boolean mActionbarOverlay = false;
	
	private static final String LOG_TAG = "SlideMenuDemo";
	
	
	private View mTitle;
	private Button mTitleShowMenuBtn;
	private View mTitleUserLayout;
	private ImageButton mTitleUserIcon;
	private View mTitleCenterTitleLayout;
	private Button mTitleSearchBtn;
	
	private View mMenu;
	private View mContentMask;
	private View mContentView;

	private int mMenuWidth;
	private int mTitleHeight = 200;
	
	private int mScreenWidth;
	private int mScreenHeight;
	
	private Context mContext;
	
	private Animation mMenuInAnimation;
	private Animation mMenuOutAnimation;
	private Animation mContentMaskInAnimation;
	private Animation mContentMaskOutAnimation;
	
	private Animation mTitleSideMenuInAnimation;
	private Animation mTitleSideMenuOutAnimation;
	private Animation mTitleUserLayoutInAnimation;
	private Animation mTitleUserLayoutOutAnimation;
//	private Animation mTitleUserIconInAnimation;
//	private Animation mTitleUserIconOutAnimation;
	private Animation mTitleCenterTitleInAnimation;
	private Animation mTitleCenterTitleOutAnimation;
	private Animation mTitleSearchBtnInAnimation;
	private Animation mTitleSearchBtnOutAnimation;
	
	private static final float CONTENT_MASK_ALPHA_MAX = 0.5f;
	private static final int ANIMATION_DURATION = 2000;

	private static final int TOUCH_STATE_REST = 0;	// do nothing
	private static final int TOUCH_STATE_SCROLLING = 1;	// scrolling screen
	private int mTouchState = TOUCH_STATE_REST;
	private Scroller mScroller;
	
	private int mTouchSlop;
	private static int SNAP_VELOCITY = 600;
	private float mScrollX = 0.0f;
	private float mLastMotionX = 0;		// last touch motion X
	private float mLastMotionY = 0;		// last touch motion Y
	
	private VelocityTracker mVelocityTracker = null;
	private static final int VELOCITY_TRACKER_UNITS = 1000;
	
	public SlideMenuDemo(Context context) {
		this(context, null);
	}
	
	public SlideMenuDemo(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public SlideMenuDemo(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
		
		mContext = context;
		
		initScrollers();
		
		setWillNotDraw(false);
	}
	
	private void initScrollers() {
		mScroller = new Scroller(mContext);
		
		mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
	}
	
	@Override
	protected void onFinishInflate() {
		// TODO Auto-generated method stub
		super.onFinishInflate();
		
		initViews();
		initAnimation();
	}
	
	private void initViews() {
		WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
		if (wm != null) {
			mScreenWidth = wm.getDefaultDisplay().getWidth();
			mScreenHeight = wm.getDefaultDisplay().getHeight();
			
			mMenuWidth = (int) (0.8 * mScreenWidth);
		}
		
		mTitle = findViewById(R.id.slide_title_layout);
		mTitleShowMenuBtn = (Button) findViewById(R.id.slide_side_menu);
		mTitleUserLayout = findViewById(R.id.slide_title_user_info);
		mTitleUserIcon = (ImageButton) findViewById(R.id.slide_title_user_icon);
		mTitleCenterTitleLayout = findViewById(R.id.slide_title_center_title_layout);
		mTitleSearchBtn = (Button) findViewById(R.id.slide_title_search);
		
		//mContentContainer = findViewById(R.id.slide_content_container);
		mContentView = findViewById(R.id.content_frame);
		mContentMask = findViewById(R.id.content_mask);
		//mMenu = findViewById(R.id.menu_frame);
		mMenu = findViewById(R.id.menu_frame);
		
		mTitleHeight = getContext().getResources().getDimensionPixelSize(R.dimen.title_height);
		
		if (mTitleShowMenuBtn != null) {
			mTitleShowMenuBtn.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View arg0) {
					
					Log.w("test", "show menu button clicked.");
					
					boolean isMenuShowing = isMenuShowing();
					Log.w("test", "isMenuShow:" + isMenuShowing);
					
					if (isMenuShowing) {
						showContent();
					} else {
						showMenu();
					}
				}
			});
		}
	
		Log.d("test", "title height:" + mTitleHeight);
	}
	
	private void initAnimation() {
		//animation = AnimationCreator.createFadeOutAnimation(1, 0);
		mMenuInAnimation = AnimationCreator.createTranslateXAnimation(-mMenuWidth, 0);
		mMenuInAnimation.setDuration(ANIMATION_DURATION);
		mMenuInAnimation.setFillAfter(true);
		
		mMenuOutAnimation = AnimationCreator.createTranslateXAnimation(0, -mMenuWidth);
		mMenuOutAnimation.setDuration(ANIMATION_DURATION);
		mMenuOutAnimation.setFillAfter(true);
		
		mContentMaskInAnimation = AnimationUtils.loadAnimation(getContext(), R.anim.slide_title_content_mask_in);
		mContentMaskInAnimation.setDuration(ANIMATION_DURATION);
		
		mContentMaskOutAnimation = AnimationUtils.loadAnimation(getContext(), R.anim.slide_title_content_mask_out);
		mContentMaskOutAnimation.setDuration(ANIMATION_DURATION);
		
		mTitleSideMenuInAnimation = AnimationUtils.loadAnimation(getContext(), R.anim.slide_title_side_menu_in);
		mTitleSideMenuInAnimation.setDuration(ANIMATION_DURATION);
		mTitleSideMenuInAnimation.setFillAfter(true);
		
		mTitleSideMenuOutAnimation = AnimationUtils.loadAnimation(getContext(), R.anim.slide_title_side_menu_out);
		mTitleSideMenuOutAnimation.setDuration(ANIMATION_DURATION);
		mTitleSideMenuOutAnimation.setFillAfter(true);
		
		mTitleUserLayoutInAnimation = AnimationUtils.loadAnimation(getContext(), R.anim.slide_title_user_in);
		mTitleUserLayoutInAnimation.setDuration(ANIMATION_DURATION);
		mTitleUserLayoutOutAnimation = AnimationUtils.loadAnimation(getContext(), R.anim.slide_title_user_out);
		mTitleUserLayoutOutAnimation.setDuration(ANIMATION_DURATION);
		
//		mTitleUserIconInAnimation = AnimationUtils.loadAnimation(getContext(), R.anim.slide_title_user_icon_in);
//		mTitleUserIconInAnimation.setDuration(ANIMATION_DURATION);
//		mTitleUserIconInAnimation.setFillAfter(true);
//		
//		mTitleUserIconOutAnimation = AnimationUtils.loadAnimation(getContext(), R.anim.slide_title_user_icon_out);
//		mTitleUserIconOutAnimation.setDuration(ANIMATION_DURATION);
//		mTitleUserIconOutAnimation.setFillAfter(true);
		
		mTitleCenterTitleInAnimation = AnimationUtils.loadAnimation(getContext(), R.anim.slide_title_center_title_in);
		mTitleCenterTitleInAnimation.setDuration(ANIMATION_DURATION);
		mTitleCenterTitleInAnimation.setFillAfter(true);
		
		mTitleCenterTitleOutAnimation = AnimationUtils.loadAnimation(getContext(), R.anim.slide_title_center_title_out);
		mTitleCenterTitleOutAnimation.setDuration(ANIMATION_DURATION);
		mTitleCenterTitleOutAnimation.setFillAfter(true);
		
		mTitleSearchBtnInAnimation = AnimationUtils.loadAnimation(getContext(), R.anim.slide_title_search_in);
		mTitleSearchBtnInAnimation.setDuration(ANIMATION_DURATION);
		mTitleSearchBtnInAnimation.setFillAfter(true);
		
		mTitleSearchBtnOutAnimation = AnimationUtils.loadAnimation(getContext(), R.anim.slide_title_search_out);
		mTitleSearchBtnOutAnimation.setDuration(ANIMATION_DURATION);
		mTitleSearchBtnOutAnimation.setFillAfter(true);
		
		//mMenuInAnimation.setRepeatCount(Animation.INFINITE);
	
		mMenuInAnimation.setAnimationListener(new AnimationListener() {
			
			@Override
			public void onAnimationStart(Animation arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onAnimationRepeat(Animation arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onAnimationEnd(Animation arg0) {
				// TODO Auto-generated method stub
				Log.e("test", "open menu layout.");
				
				Log.w("test", "left:" + mMenu.getLeft() + ",right:" + mMenu.getRight());
				Log.w("test", "X:" + mMenu.getX() + ",Y:" + mMenu.getY());
				
				//mMenu.setDrawingCacheEnabled(true);
				
				//mMenu.layout(-mMenuWidth, mTitleHeight, 0, mScreenHeight);
				
				onOpenMenu();
			}
		});
		
		mMenuOutAnimation.setAnimationListener(new AnimationListener() {
			
			@Override
			public void onAnimationStart(Animation arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onAnimationRepeat(Animation arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onAnimationEnd(Animation arg0) {
				// TODO Auto-generated method stub
				Log.e("test", "close menu layout.");
				
				Log.w("test", "left:" + mMenu.getLeft() + ",right:" + mMenu.getRight());
				Log.w("test", "X:" + mMenu.getX() + ",Y:" + mMenu.getY());
				
				//mMenu.layout(-mMenuWidth, mTitleHeight, 0, mScreenHeight);
				mMenu.offsetLeftAndRight(-mMenuWidth);
				
				onCloseMenu();
			}
		});
		
		mTitleSideMenuInAnimation.setAnimationListener(new AnimationListener() {
			
			@Override
			public void onAnimationStart(Animation arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onAnimationRepeat(Animation arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onAnimationEnd(Animation arg0) {
				// TODO Auto-generated method stub
//				mTitleShowMenuBtn.setWidth(mTitleShowMenuBtn.getWidth() / 2);
//				mTitleShowMenuBtn.layout(0, 0, mTitleShowMenuBtn.getWidth(), mTitleShowMenuBtn.getHeight());
			}
		});
		
		mTitleSideMenuOutAnimation.setAnimationListener(new AnimationListener() {
			
			@Override
			public void onAnimationStart(Animation arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onAnimationRepeat(Animation arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onAnimationEnd(Animation arg0) {
				// TODO Auto-generated method stub
//				mTitleShowMenuBtn.setWidth(mTitleShowMenuBtn.getWidth() * 2);
//				mTitleShowMenuBtn.layout(0, 0, mTitleShowMenuBtn.getWidth(), mTitleShowMenuBtn.getHeight());
			}
		});
		
		mTitleUserLayoutOutAnimation.setAnimationListener(new AnimationListener() {
			
			@Override
			public void onAnimationStart(Animation arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onAnimationRepeat(Animation arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onAnimationEnd(Animation arg0) {
				// TODO Auto-generated method stub
				if (mTitleUserLayout.getVisibility() == View.VISIBLE) {
					mTitleUserLayout.setVisibility(View.GONE);
				}
			}
		});
		
//		mTitleUserIconInAnimation.setAnimationListener(new AnimationListener() {
//			
//			@Override
//			public void onAnimationStart(Animation arg0) {
//				// TODO Auto-generated method stub
//			}
//			
//			@Override
//			public void onAnimationRepeat(Animation arg0) {
//				// TODO Auto-generated method stub
//				
//			}
//			
//			@Override
//			public void onAnimationEnd(Animation arg0) {
//				// TODO Auto-generated method stub
//				mTitleUserIcon.setAlpha(1.0f);
////				float offsetLeft = (float) (-0.4 * mTitleUserIcon.getWidth());
////				mTitleUserIcon.offsetLeftAndRight((int) offsetLeft);
//			}
//		});
//		
//		mTitleUserIconOutAnimation.setAnimationListener(new AnimationListener() {
//			
//			@Override
//			public void onAnimationStart(Animation arg0) {
//				// TODO Auto-generated method stub
//				
//			}
//			
//			@Override
//			public void onAnimationRepeat(Animation arg0) {
//				// TODO Auto-generated method stub
//				
//			}
//			
//			@Override
//			public void onAnimationEnd(Animation arg0) {
//				// TODO Auto-generated method stub
//				mTitleUserIcon.setAlpha(0.5f);
////				float offsetLeft = (float) (0.4 * mTitleUserIcon.getWidth());
////				mTitleUserIcon.offsetLeftAndRight((int) offsetLeft);
//			}
//		});
	}
	
	public void showMenu() {
		if (isMenuShowing()) {
			return;
		}
		
		if (mContentMask.getVisibility() == View.INVISIBLE) {
			mContentMask.setVisibility(View.VISIBLE);
		}
		//mMenu.setDrawingCacheEnabled(false);
		mContentMask.startAnimation(mContentMaskInAnimation);
		
		mMenu.offsetLeftAndRight(mMenuWidth);
		mMenu.startAnimation(mMenuInAnimation);
		
		mTitleShowMenuBtn.startAnimation(mTitleSideMenuInAnimation);
		
		if (mTitleUserLayout.getVisibility() != View.VISIBLE) {
			mTitleUserLayout.setVisibility(View.VISIBLE);
		}
		mTitleUserLayout.startAnimation(mTitleUserLayoutInAnimation);
		
		
		if (mTitleUserIcon.getVisibility() == View.INVISIBLE) {
			mTitleUserIcon.setVisibility(View.VISIBLE);
		}
		
//		mTitleUserIconInAnimation.setFillBefore(true);
//		mTitleUserIcon.startAnimation(mTitleUserIconInAnimation);
		
		mTitleCenterTitleLayout.startAnimation(mTitleCenterTitleOutAnimation);
		mTitleSearchBtn.startAnimation(mTitleSearchBtnOutAnimation);
	}
	
	public boolean isMenuShowing() {
		Log.w("test", "isMenuShowing, menuLeft:" + mMenu.getLeft() + ",menuRight=" + mMenu.getRight());
//		if (mMenu.getLeft() >= 0 || mMenu.getRight() >= mMenuWidth) {
//			return true;
//		}
		if (mMenu.getLeft() >= 0) {
			return true;
		}
		return false;
	}
	
	private void onOpenMenu() {
		//mContentMask.setAlpha(CONTENT_MASK_ALPHA_MAX);
	}
	
	private void onCloseMenu() {
		if (mContentMask.getVisibility() == View.VISIBLE) {
			mContentMask.setVisibility(View.INVISIBLE);
		}
	}
	
	public void showContent() {
		if (!isMenuShowing()) {
			return;
		}
		
		mContentMask.startAnimation(mContentMaskOutAnimation);
		
		//mMenu.offsetLeftAndRight(-mMenuWidth);
		mMenu.startAnimation(mMenuOutAnimation);
		
		mTitleShowMenuBtn.startAnimation(mTitleSideMenuOutAnimation);
		mTitleUserLayout.startAnimation(mTitleUserLayoutOutAnimation);
//		mTitleUserIcon.startAnimation(mTitleUserIconOutAnimation);
		
		mTitleCenterTitleLayout.startAnimation(mTitleCenterTitleInAnimation);
		mTitleSearchBtn.startAnimation(mTitleSearchBtnInAnimation);
	}
	
	public void attachToActivity(Activity activity, int slideStyle) {
		attachToActivity(activity, slideStyle, true);
	}
	
	public void attachToActivity(Activity activity, int slideStyle, boolean actionbarOverlay) {
		if (getParent() != null) {
			throw new IllegalStateException("This Slide menu appears to already be attached.");
		}
		
		TypedArray a = activity.getTheme().obtainStyledAttributes(new int[] { android.R.attr.windowBackground });
		int background = a.getResourceId(0, 0);
		a.recycle();
		
		switch (slideStyle) {
		case SLIDING_WINDOW:
			mActionbarOverlay = false;
			ViewGroup decor = (ViewGroup) activity.getWindow().getDecorView();
			ViewGroup decorChild = (ViewGroup) decor.getChildAt(0);
			decorChild.setBackgroundResource(background);
			decor.removeView(decorChild);
			decor.addView(this);
			break;
		case SLIDING_CONTENT:
			mActionbarOverlay = actionbarOverlay;
			ViewGroup contentParent = (ViewGroup) activity.findViewById(android.R.id.content);
			View content = contentParent.getChildAt(0);
			contentParent.removeView(content);
			contentParent.addView(this);
//			if (content.getBackground() == null) {
//				content.setBackgroundResource(background);
//			}
			break;
		}
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		
		Log.w("test", "onMeasure begin.");
		
		// TODO Auto-generated method stub
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		
		int measuredWidth = measureWidth(widthMeasureSpec);
		int measuredHeight = measureHeight(heightMeasureSpec);
		
		//measureChildren(widthMeasureSpec, heightMeasureSpec);
		measureChild(mTitle, widthMeasureSpec, heightMeasureSpec);
		measureChild(mMenu, MeasureSpec.makeMeasureSpec(mMenuWidth, MeasureSpec.EXACTLY), heightMeasureSpec);
		measureChild(mContentMask, widthMeasureSpec, heightMeasureSpec);
		measureChild(mContentView, widthMeasureSpec, heightMeasureSpec);
		
		setMeasuredDimension(measuredWidth, measuredHeight);
		
		Log.w("test", "onMeasure end.");
	}
	
	private int measureWidth(int pWidthMeasureSpec) {
		int result = 0;
		
		int widthMode = MeasureSpec.getMode(pWidthMeasureSpec);
		int widthSize = MeasureSpec.getSize(pWidthMeasureSpec);
	
		switch (widthMode) {
		case MeasureSpec.UNSPECIFIED:
			break;
		case MeasureSpec.EXACTLY:
		case MeasureSpec.AT_MOST:
			result = widthSize;
			break;
		}
		return result;
	}
	
	private int measureHeight(int pHeightMeasureSpec) {
		int result = 0;
		
		int heightMode = MeasureSpec.getMode(pHeightMeasureSpec);
		int heightSize = MeasureSpec.getSize(pHeightMeasureSpec);
		
		switch (heightMode) {
		case MeasureSpec.UNSPECIFIED:
			break;
		case MeasureSpec.EXACTLY:
		case MeasureSpec.AT_MOST:
			result = heightSize;
			break;
		}
		return result;
	}

	@Override
	protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
		// TODO Auto-generated method stub
		Log.w("test", "onLayout begin.");
		
		Log.w("test", "changed=" + changed);
		
		if (!changed) {
			return;
		}
		
		mTitle.layout(0, 0, mScreenWidth, mTitleHeight);
//		mContentContainer.layout(0, mTitleHeight, mScreenWidth, mScreenHeight);
//		mMenu.layout(-mMenuWidth, 0, 0, mScreenHeight - mTitleHeight);
//		mContentView.layout(0, 0, mScreenWidth, mScreenHeight - mTitleHeight);
//		mContentMask.layout(0, 0, mScreenWidth, mScreenHeight - mTitleHeight);
		mMenu.layout(-mMenuWidth, mTitleHeight, 0, mScreenHeight);
		mContentView.layout(0, mTitleHeight, mScreenWidth, mScreenHeight);
		mContentMask.layout(0, mTitleHeight, mScreenWidth, mScreenHeight);
		
		Log.w("test", "onLayout end.");
	}
	
	@Override
	protected void dispatchDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		Log.v("test", "dispatchDraw begin.");
		
		super.dispatchDraw(canvas);
	}
	
	@Override
	protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
		// TODO Auto-generated method stub
		
		Log.v("test", "drawChild begin.");
		
		return super.drawChild(canvas, child, drawingTime);
	}
	
	@Override
	public void computeScroll() {
		// TODO Auto-generated method stub
		Log.e(LOG_TAG, "computeScroll begin.");
		
		//scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
		
		if (mScroller.computeScrollOffset()) {
			mMenu.scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
			
			postInvalidate();
		} else {
			Log.e(LOG_TAG, "not scroll.");
		}
		
	}
	
	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {
		// TODO Auto-generated method stub
		Log.i("test", "onInterceptTouchEvent begin. slop=" + mTouchSlop);
		
		final int action = ev.getAction();
		
		if ((action == MotionEvent.ACTION_MOVE) && (mTouchState != TOUCH_STATE_REST)) {
			return true;
		}
		
		final float x = ev.getX();
		final float y = ev.getY();
		
		switch (action) {
		case MotionEvent.ACTION_MOVE:
			Log.d("test", "onInterceptTouchEvent move.");
			
			final int xDiff = (int) Math.abs(mLastMotionX - x);
			if (xDiff > mTouchSlop) {
				mTouchState = TOUCH_STATE_SCROLLING;
			}
			break;
		case MotionEvent.ACTION_DOWN:
			Log.d("test", "onInterceptTouchEvent down.");
			
			mLastMotionX = x;
			mLastMotionY = y;
			
			mTouchState = mScroller.isFinished() ? TOUCH_STATE_REST
					: TOUCH_STATE_SCROLLING;
			
			break;
		case MotionEvent.ACTION_UP:
		case MotionEvent.ACTION_CANCEL:
			Log.d("test", "onInterceptTouchEvent up or cancel.");
			
			mTouchState = TOUCH_STATE_REST;
			break;
		}
		
		Log.i("test", "mTouchState=" + mTouchState);
		
		return mTouchState != TOUCH_STATE_REST;
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		super.onTouchEvent(event);
		
		Log.w("test", "onTouchEvent begin.");
		
		if (mVelocityTracker == null) {
			mVelocityTracker = VelocityTracker.obtain();
		}
		mVelocityTracker.addMovement(event);
		
		float x = event.getX();
		float y = event.getY();
		
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
//			if (mScroller != null && !mScroller.isFinished()) {
//				mScroller.abortAnimation();
//			}
			mLastMotionX = x;
			break;
		case MotionEvent.ACTION_MOVE:
			int deltaX = (int) (mLastMotionX - x);
			
			//scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
			Log.i(LOG_TAG, "menu scrollX=" + mMenu.getScrollX() + ",x=" + x + ",lastX=" + mLastMotionX);
			
			int scrollX = mMenu.getScrollX();
			if (deltaX < 0 && scrollX < -mMenuWidth) {
				Log.e(LOG_TAG, "1");
				deltaX = -mMenuWidth - scrollX;
			} else if (deltaX > 0 && scrollX >= mMenuWidth) {
				Log.e(LOG_TAG, "2");
				deltaX = 0;
			}
			
			mLastMotionX = x;
			
			Log.e(LOG_TAG, "deltaX=" + deltaX);
			
			mMenu.scrollBy(deltaX, 0);
			postInvalidate();
			
			break;
		case MotionEvent.ACTION_UP:
			final VelocityTracker velocityTracker = mVelocityTracker;
			velocityTracker.computeCurrentVelocity(VELOCITY_TRACKER_UNITS);
			
			int velocityX = (int) velocityTracker.getXVelocity();
			
			Log.d(LOG_TAG, "velocity x =" + velocityX);
			
			if (velocityX > SNAP_VELOCITY) {
				Log.d(LOG_TAG, "flick right.");
//				showMenu();
			} else if (velocityX < -SNAP_VELOCITY) {
				Log.d(LOG_TAG, "flick left.");
//				showContent();
			} else {
				//snapToDestination();
			}
			
			if (mVelocityTracker != null) {
				mVelocityTracker.recycle();
				mVelocityTracker = null;
			}
			
			mTouchState = TOUCH_STATE_REST;
			break;
		case MotionEvent.ACTION_CANCEL:
			mTouchState = TOUCH_STATE_REST;
			break;
		}
		
		return true;
	}
	
	private void snapToDestination() {
		int scrollX = getScrollX();
		int scrollY = getScrollY();
		
		Log.i(LOG_TAG, "onTouchEvent snapToDestion, scrollX="
				+ scrollX + ",scrollY=" + scrollY);
		
		if (scrollX < mMenuWidth / 2) {
			showMenu();
		} else {
			showContent();
		}
	}
}
